package org.itboys.pay.demo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.pay.alipay.AlipayWapResult;
import org.itboys.pay.weixin.WXPayRefund;
import org.itboys.pay.weixin.WXPayService;
import org.itboys.pay.weixin.WeixinOrderQuery;
import org.itboys.pay.weixin.WeixinPaySignTool;
import org.itboys.pay.weixin.WeixinPayUtils;

/** 
 * @author 作者 huml 
 * @version 创建时间：2016-4-1 下午1:39:58 
 * 微信支付demo 
 */
public class CoreWeiXinPayDemo extends BaseController {

	/**
	 * 获取支付所需要的参数
	 * 包含统一下单和签名等操作
	 */
	public void getPayParam(){
		String appid = "";//微信appid
		String mchId = "";//微信商户号
		String openid = "";//openid
		String outTradeNo = "";//订单号
		String spbillCreateIp = "";//APP和网页支付提交用户端ip，Native支付填调用微信支付API的机器IP 必须
		String tradeType = "";//交易类型 trade_type=JSAPI表示H5页面发起调用 必须  JSAPI APP NATIVE(扫码支付)
		String body = "";//商品或支付单简要描述 必须
		String totalFee = null;//订单总金额,只能为整数,单位为分 必须
		String notifyUrl = "";//异步回调链接
		String key = "";//支付秘钥
		try {
//			WeixinPaySignTool weixinPaySignTool = WeixinPayUtils.paySign(appid, mchId, openid, outTradeNo, spbillCreateIp, tradeType, body, totalFee, notifyUrl, key);
			//WeixinPaySignTool里面含有最终支付所需要的参数,可以把这个对象返回给手机端
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 异步回调
	 * @param request
	 * @param response
	 */
	public void notify(HttpServletRequest request, HttpServletResponse response){
		try {
			AlipayWapResult wapResult = new AlipayWapResult();
			String key = "";//支付key 
			wapResult = WXPayService.notifyResult(request, key);
			if (!wapResult.isVerify_result()) {
				AjaxUtils.renderText(response, CommonConstants.FAIL);
				return;
			}
			//    TODO
			//签名校验通过之后,验证支付金额及修改订单状态等操作
			
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("completePayForWeiXin error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	/**
	 * 查询订单状态
	 */
	public void weixinOrderQuery(){
		try {
			String outTradeNo = ""; //订单号
			String appid = ""; //appid
			String mchId = ""; //商户号
			String key = "";//支付key
			String state = WXPayService.orderQuery(outTradeNo, appid, mchId, key);
			if(state.equals(WeixinOrderQuery.SUCCESS)){
				//支付成功
			} else if (state.equals(WeixinOrderQuery.REFUND)) {
				//转入退款
			} else if (state.equals(WeixinOrderQuery.NOTPAY)) {
				//未支付
			} else if (state.equals(WeixinOrderQuery.CLOSED)) {
				//交易关闭
			} else if (state.equals(WeixinOrderQuery.REVOKED)) {
				//已撤销（刷卡支付）
			} else if (state.equals(WeixinOrderQuery.USERPAYING)) {
				//用户支付中
			} else if (state.equals(WeixinOrderQuery.PAYERROR)) {
				//支付失败(其他原因，如银行返回失败)
			} else {
				//查询出错
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	/**
	 * 申请退款
	 */
	public void weixinRefund(){
		String outTradeNo = ""; //我们传给微信的订单号
		String transactionId = "";//微信返回过来的他们自己 的交易流水号
		//transactionId与outTradeNo两个只需传其中一个
		String appid = ""; //appid
		String mchId = ""; //商户号
		String key = "";//支付key
		String outFefundNo = "";//我们自己的退款编号
		int totalFee = 0;
		Integer refundFee = null;//退款金额
		String filePath = "";//证书
		try {
			String result = WXPayRefund.weixinRefund(appid, mchId, key, transactionId, outTradeNo, outFefundNo, totalFee, refundFee, filePath);
			if(result.equals(CommonConstants.SUCCESS)){
				//成功
			} else {
				//失败
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 微信扫码支付
	 */
	public void nativePay(){
		/*try {
			String weixinPaySignTool = WeixinPayUtils.nativePay("wxe00e3899f4633ecd", "1403007302", "11111111111", "127.0.0.1", "test",
					"1", "http://admin.fuzion.com", "1", "123456789000000zsmzsmzsmzsm00000", "C:\\Users\\Administrator\\Desktop");
			logger.info("*******************" + JSON.toJSONString(weixinPaySignTool));
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}
	
}
